#include "test.h"
#include "ui_test.h"
#include "utils.h"
#include "studentstore.h"
#include <vector>
#include <iostream>
#include <QMessageBox>
using namespace utils;

using namespace student;
using namespace studentstore;
test::test(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::test)
{
    ui->setupUi(this);
    reviseo=new revise(this);
    reviseo->hide();
    QStringList header;
    ui->tableWidget->setColumnCount(6);
    header << "ID" << "书名" << "ISBN" <<"总数量"<<"删除"<<"修改";
    ui->tableWidget->setHorizontalHeaderLabels(header);

    QStringList header_stu;

    page=0;
    pagesize=2;
    connect(this,SIGNAL(sendData(Student*)),reviseo,SLOT(getData(Student*)));
    //connect(reviseo,SIGNAL(sendData(QString)),this,SLOT(getData(QString)));


}

test::~test()
{
    delete ui;
}
bool test::del(int ID){
    if(deleteStudent(ID))
    {
        QMessageBox::information(this,"title","成功");
        return 1;
    }
    else
        return 0;

}
bool test::re(Student *stu){
    if(addStudent(stu))
    {
        QMessageBox::information(this,"title","成功");
        return 1;
    }
    else
        return 0;

}
void test::show(vector<Menu*>menulist) {

    for (int i = 0; i < menulist.size(); i++) {      //展示信息

        ui->tableWidget->setRowCount(menulist.size());   //设置表格行数

        QString tempCell = QString(QString::number(menulist[i]->ID));
        ui->tableWidget->setItem(i, 0, new QTableWidgetItem(tempCell));
        tempCell = QString(QString::fromLocal8Bit(menulist[i]->name));
        ui->tableWidget->setItem(i, 1, new QTableWidgetItem(tempCell));
        tempCell = QString(QString::fromLocal8Bit(menulist[i]->ISBN));
        ui->tableWidget->setItem(i, 2, new QTableWidgetItem(tempCell));
        tempCell =  QString(QString::number(menulist[i]->countNum));
        ui->tableWidget->setItem(i, 3, new QTableWidgetItem(tempCell));

    }
}

void test::showstu_(vector<Student*>stuList,int confnum=0){

    int pagenum=(stuList.size()+pagesize-1)/pagesize;
    int i = confnum*pagesize;
   // for (int i = confnum*pagesize; i < (confnum+1)*pagesize; i++) {      //展示信息
        QString tempCell = QString(QString::number(stuList[i]->ID));
        ui->lineEdit_5->setText(tempCell);
        tempCell = QString(QString::fromLocal8Bit(stuList[i]->name));
        ui->lineEdit_6->setText(tempCell);
        tempCell = QString(QString::fromLocal8Bit(stuList[i]->stu_id));
        ui->lineEdit_7->setText(tempCell);
        tempCell = QString(QString::fromLocal8Bit(stuList[i]->major));
        ui->lineEdit_8->setText(tempCell);
        tempCell = QString(QString::number(stuList[i+1]->ID));
        ui->lineEdit_9->setText(tempCell);
        tempCell = QString(QString::fromLocal8Bit(stuList[i+1]->name));
        ui->lineEdit_10->setText(tempCell);
        tempCell = QString(QString::fromLocal8Bit(stuList[i+1]->stu_id));
        ui->lineEdit_11->setText(tempCell);
        tempCell = QString(QString::fromLocal8Bit(stuList[i+1]->major));
        ui->lineEdit_12->setText(tempCell);
        tempCell = QString(QString::number(pagenum));
        ui->label_3->setText(tempCell);
        tempCell = QString(QString::number(confnum));
        ui->lineEdit_13->setText(tempCell);
    //}
}


void test::on_pushButton_7_clicked()//点击查询
{
    QString str = ui->lineEdit->text(); //Qstring与int转化
    string searchKey = string((const char *)str.toLocal8Bit());
    vector<Student*> stuList = search_multipleReturn<Student>("useFuzzySearch", "name", searchKey, studentDatPath) ;
    this->showstu_(stuList,0);

}


void test::on_pushButton_17_clicked()
{
    QString str_ = ui->lineEdit_13->text();
    int confnum =str_.toInt();
    page=confnum;
    QString str = ui->lineEdit->text(); //Qstring与int转化
    string searchKey = string((const char *)str.toLocal8Bit());
    vector<Student*> stuList = search_multipleReturn<Student>("useFuzzySearch", "name", searchKey, studentDatPath) ;
    this->showstu_(stuList,confnum);
}

void test::on_pushButton_5_clicked()
{
    int confnum =page-1;
    page--;
    QString str = ui->lineEdit->text(); //Qstring与int转化
    string searchKey = string((const char *)str.toLocal8Bit());
    vector<Student*> stuList = search_multipleReturn<Student>("useFuzzySearch", "name", searchKey, studentDatPath) ;
    this->showstu_(stuList,confnum);
}

void test::on_pushButton_6_clicked()
{
    int confnum =page+1;
    page++;
    QString str = ui->lineEdit->text(); //Qstring与int转化
    string searchKey = string((const char *)str.toLocal8Bit());
    vector<Student*> stuList = search_multipleReturn<Student>("useFuzzySearch", "name", searchKey, studentDatPath) ;
    this->showstu_(stuList,confnum);
}

void test::on_pushButton_13_clicked()
{
    QString str = ui->lineEdit->text(); //Qstring与int转化
    string searchKey = string((const char *)str.toLocal8Bit());
    vector<Student*> stuList = search_multipleReturn<Student>("useFuzzySearch", "name", searchKey, studentDatPath) ;
    Student* stu=stuList[page*pagesize];
    emit sendData(stu);
    reviseo->open();
}


void test::on_pushButton_9_clicked()//查找书籍
{
    QString str = ui->lineEdit->text(); //Qstring与int转化
    string searchKey = string((const char *)str.toLocal8Bit());
    //vector<Menu*> menuList = search_multipleReturn<Menu>("useFuzzySearch", "name", searchKey, menuDatPath) ;

}

void test::on_pushButton_18_clicked()
{
    QString str = ui->lineEdit->text(); //Qstring与int转化
    string searchKey = string((const char *)str.toLocal8Bit());
    //vector<Book*> bookList = search_multipleReturn<Book>("notFuzzySearch", "ISBN", searchKey, bookDatPath) ;
    //返回所有该类book

}
